package com.study.demo.es;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class JavaEsTest {

    private String IP;
    private int PORT;

    @Before
    public void init(){
        this.IP = "10.0.0.28";
        this.PORT = 5601;
    }

//    @Test
//    public void esClient(){
//        try {
//            Settings settings = Settings.builder().put("cluster.name", "dsl-cluster").build();
//
//            TransportClient client = new PreBuiltTransportClient(settings)
//                    .addTransportAddresses(new TransportAddress(InetAddress.getByName("10.0.0.28"), 9200));
//            System.out.println(client.toString());
//        } catch (UnknownHostException e) {
//            e.printStackTrace();
//        }
//    }



//    @Test
//    public void test()
//    {
//        // 阿里云ES集群需要basic auth验证。
//        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//
//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "123456"));
//        RestClientBuilder builder = RestClient.builder(new HttpHost("10.0.0.28", 9200)).setHttpClientConfigCallback(
//                new RestClientBuilder.HttpClientConfigCallback() {
//                    @Override
//                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                    }
//                });
//
//
//        System.out.printf(builder.toString());
//    }
}
